package com.marketingexpert.controller.marketingmgnt.autoMarketing;

import java.io.PrintWriter;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.Controller;

import com.marketingexpert.domain.AutoatContent;
import com.marketingexpert.domain.User;
import com.marketingexpert.exception.MarketingExpertException;
import com.marketingexpert.service.marketingmgnt.MarketingService;
import com.marketingexpert.service.usermgnt.UserService;
import com.marketingexpert.util.Resources;

public class SaveAtContentController implements Controller {

	private MarketingService marketingService;
	
	private UserService userService;
	
	@Override
	public ModelAndView handleRequest(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		
		response.setContentType("text/html;charset=GBK"); 
		response.setHeader("Pragma", "No-cache"); 
		response.setHeader("Cache-Control", "no-cache"); 
		response.setHeader("Cache-Control", "no-store"); 
		response.setDateHeader("Expires", 0);  
		PrintWriter out = response.getWriter();
		
		String atContent = request.getParameter("atContent");
		String contentId = request.getParameter("contentId");
		
		String parentUserId = (String) request.getSession().getAttribute("parentUserId");
		try {
			User user = getUserService().queryUserByUId(parentUserId);
			AutoatContent ac = new AutoatContent(atContent, user.getId());
			int acId = -1;
			if(contentId == null || contentId.trim().equals("")) {
				acId = marketingService.saveAutoAtContent(ac);
			} else {
				ac.setId(Integer.parseInt(contentId));
				marketingService.updateAtcontent(ac);
				acId = Integer.parseInt(contentId);
			}	
			out.print(acId);
			return null;
		} catch(MarketingExpertException e) {
			throw e;
		}
	}

	public MarketingService getMarketingService() {
		return marketingService;
	}

	public void setMarketingService(MarketingService marketingService) {
		this.marketingService = marketingService;
	}

	public UserService getUserService() {
		return userService;
	}

	public void setUserService(UserService userService) {
		this.userService = userService;
	}
	
	

}
